package com.ttai.ui.activity;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.igexin.sdk.PushManager;
import com.ttai.R;
import com.ttai.presenter.activity.OTPCreatePresenter;
import com.ttai.presenter.activity.TaiIdPresenter;
import com.ttai.ui.adapter.LoginIdRecycleViewAdapter;
import com.ttai.ui.base.BaseActivity;
import com.ttai.untils.Constant;
import com.ttai.untils.ItemClickSupport;

import butterknife.Bind;
import butterknife.ButterKnife;

public class TaiId extends BaseActivity {
    @Bind(R.id.idacount)
    RecyclerView idacount;
    LoginIdRecycleViewAdapter loginIdRecycleViewAdapter;
    TaiIdPresenter taiIdPresenter;
    OTPCreatePresenter otpCreatePresenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.avtivity_taiid);
        ButterKnife.bind(this);

        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //透明导航栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        TextView barTitle = (TextView)findViewById(R.id.barTitle);
        barTitle.setText("账号选择");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        taiIdPresenter = new TaiIdPresenter(this);
        otpCreatePresenter = new OTPCreatePresenter(this);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);

        loginIdRecycleViewAdapter = new LoginIdRecycleViewAdapter(this);
        idacount.setAdapter(loginIdRecycleViewAdapter);
        idacount.setLayoutManager(new GridLayoutManager(this, 3));

//        loginIdPresenter.getData();
        taiIdPresenter.storeIdAcount(Constant.getToken());

        ItemClickSupport.addTo(idacount).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {

            private String appName;
            private String acountlogo;

            @Override
            public void onItemClicked(RecyclerView recyclerView, int position, View v) {
                appName = taiIdPresenter.getList().get(position).getId_name();
                acountlogo = taiIdPresenter.getList().get(position).getAcountnumberlogo();
                Toast.makeText(TaiId.this, appName, Toast.LENGTH_SHORT).show();
                String token = Constant.getToken();
                String cid = PushManager.getInstance().getClientid(TaiId.this);
                System.out.println("cid" + cid + " token:" + token + " logo:" + acountlogo);
                otpCreatePresenter.crateOTP(token, cid, acountlogo, "1");
            }
        });
    }

    @Override
    public void next(View view) {

    }

    @Override
    public void pre(View view) {

    }

    @Override
    public void setComponet() {
        this.finish();
    }

    public LoginIdRecycleViewAdapter getLoginIdRecycleViewAdapter() {
        return loginIdRecycleViewAdapter;
    }

    public void getOTP(String otp) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        ComponentName componentName = new ComponentName("com.ttai.myapptest1", "com.ttai.myapptest1.TaiLogin");
        intent.setComponent(componentName);
        intent.putExtra("otp", otp);
        startActivity(intent);
    }
}
